/**
 * Copyright (c) 2018 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package com.community.common.core.exception;

import com.community.common.core.utils.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;


/**
 * 异常处理器
 *
 * @author Mark sunlightcs@gmail.com
 * @since 1.0.0
 */
@RestControllerAdvice
public class CommExceptionHandler {
    private static final Logger logger = LoggerFactory.getLogger(CommExceptionHandler.class);

    /**
     * 处理自定义异常
     */
    @ExceptionHandler(CommunityException.class)
    public Result handleRenException(CommunityException ex) {
        Result result = new Result();
        result.error(ex.getCode(), ex.getMsg());
        return result;
    }

    @ExceptionHandler(Exception.class)
    public Result handleException(Exception ex) {
        logger.error(ex.getMessage(), ex);
        saveLog(ex);
        return new Result().error();
    }

    /**
     * 保存异常日志
     */
    public void saveLog(Exception ex) {
    }
}